package com.lwt.hospital.service.impl;

import com.lwt.hospital.dao.DemoDao;
import com.lwt.hospital.model.*;
import com.lwt.hospital.service.DoctorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.print.Doc;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 丁兆顺
 * @date 2020/5/19 21:12
 * @desciption
 */
@Service
public class DoctorServiceImpl implements DoctorService {

    @Resource
    private DemoDao demoDao;

    @Override
    public int changePassword(Doctor doctor, HttpSession session) {
        Doctor userInfo = (Doctor) session.getAttribute("userInfo");
        if (!doctor.getOldPassword().equals(userInfo.getPassword())) {
            return -1;
        } else {
            return demoDao.changePassword(userInfo.getId(), doctor.getPassword());
        }
    }

    @Override
    public List<Hospitalized> getPatient(HttpSession session) {
        Doctor userInfo = (Doctor) session.getAttribute("userInfo");
        List<Hospitalized> patient = demoDao.getPatient(userInfo.getRoom());
        patient.forEach(item -> {
            User user = new User();
            user.setId(item.getUser());
            item.setUserName(demoDao.selectUserInfo(user).getName());
        });
        return patient;
    }


    @Override
    public int outHospital(long id) {
        return demoDao.outHospital(id);
    }

    @Override
    public List<Appointment> getAppointment(HttpSession session) {
        Doctor userInfo = (Doctor) session.getAttribute("userInfo");
        List<Appointment> appointment = demoDao.getAppointment(userInfo.getRoom());
        appointment.forEach(item -> {
            User user = new User();
            user.setId(item.getUser());
            User user1 = demoDao.selectUserInfo(user);
            item.setUserName(user1.getName());
            item.setAge(user1.getAge());
            item.setSex(user1.getSex());
        });
        return appointment;
    }

    @Override
    public Appointment getAAppointment(Appointment appointment, HttpSession session) {
        Appointment sim = demoDao.getAAppointment(appointment.getId());
        User user = new User();
        user.setId(sim.getUser());
        User user1 = demoDao.selectUserInfo(user);
        sim.setAge(user1.getAge());
        sim.setSex(user1.getSex());
        sim.setUserName(user1.getName());
        return sim;
    }

    @Override
    public int modifyAppointment(Appointment appointment, HttpSession session) {
        Doctor doctor = (Doctor) session.getAttribute("userInfo");
        appointment.setDoctor(doctor.getId());
        if (appointment.getIfHosp() == 1) {
            Appointment sim = demoDao.getAAppointment(appointment.getId());
            Hospitalized hospitalized = new Hospitalized();
            hospitalized.setUser(sim.getUser());
            hospitalized.setRoom(doctor.getRoom());
            Integer result = demoDao.insertHospital(hospitalized);
            if (result == 0) {
                return -1;
            }
        }
        Integer result2 = demoDao.modifyAppointment(appointment);
        return result2;
    }

    @Override
    public List<Question> getQuestion(HttpSession session){
        Doctor userInfo = (Doctor) session.getAttribute("userInfo");
        List<Question> question = demoDao.getQuestion(userInfo.getRoom());
        question.forEach(item->{
            User user = new User();
            user.setId(item.getUser());
            User user1 = demoDao.selectUserInfo(user);
            item.setUserName(user1.getName());
            item.setAge(user1.getAge());
            item.setSex(user1.getSex());
        });
        return question;
    }

    @Override
    public Question getAQuestion(Question question,HttpSession session){
        Question item = demoDao.getAQuestion(question.getId());
        User user = new User();
        user.setId(item.getUser());
        User user1 = demoDao.selectUserInfo(user);
        item.setUserName(user1.getName());
        item.setAge(user1.getAge());
        item.setSex(user1.getSex());
        return item;
    }

    @Override
    public int updateQuestion(Question question,HttpSession session){
        Doctor doctor = (Doctor) session.getAttribute("userInfo");
        question.setDoctor(doctor.getId());
        return demoDao.updateQuestion(question);
    }


    @Override
    public List<Doctor> selectAllDoc(){
        return demoDao.selectAllDoc();
    }

    @Override
    public int delDoc(Doctor doctor,HttpSession session){
        return demoDao.delDoc(doctor.getId());
    }

    @Override
    public int addDoc(Doctor doctor,HttpSession session){
        doctor.setNumber(String.valueOf(demoDao.selectMaxNumber() + 1));
        return demoDao.insertDoc(doctor);
    }

    @Override
    public Doctor selectDocById(Doctor doctor){
        return demoDao.selectDocById(doctor.getId());
    }

    @Override
    public int updateDoc(Doctor doctor){
        return demoDao.updateDoc(doctor);
    }

}
